package info.hexin.jdk.shutdownhook;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by infi.he on 2015/4/27.
 */
public class ShutdownHookTest {
    static AtomicLong atomicLong = new AtomicLong(0);

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("test 111111");
            }
        });
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("test 22222");
            }
        });

        //增加任务
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    long countlong = atomicLong.incrementAndGet();
                    for (int i = 0; i < 100; i++) {
//                        System.out.println(i);
                    }
//                    System.out.println("xxxxxxxxxx>>>>" + countlong);
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                        int count = Thread.activeCount();
                        System.out.println(Thread.activeCount());
                        if (count > 0) {
                            System.out.println(Thread.getAllStackTraces());
                            System.exit(0);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

    }
}
